package leetcode.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * AC
 */
class Foo {

    private AtomicInteger atomicInteger1 = new AtomicInteger(0);
    private AtomicInteger atomicInteger2 = new AtomicInteger(0);

    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        atomicInteger1.getAndIncrement();
    }

    public void second(Runnable printSecond) throws InterruptedException {

        while (atomicInteger1.get() != 1) {
            // 等待
        }

        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        atomicInteger2.getAndIncrement();
    }

    public void third(Runnable printThird) throws InterruptedException {

        while (atomicInteger2.get() != 1) {
            // 等待
        }

        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }
}

public class leetcode1114 {
    public static void main(String[] args) throws Exception {
        int[] nums = {1, 2, 3};
        int[] nums1 = {3, 2, 1};
        int[] nums2 = {1, 3, 2};

        test1114(nums);

        sleepsometime(1500);
        test1114(nums1);

        sleepsometime(1500);
        test1114(nums2);
    }

    private static void test1114(int[] nums) throws Exception {

        Foo foo = new Foo();

        int sleepTime = 300;

//        foo.init();

        for (int i = 0; i < 3; i++) {
            if (nums[i] == 1) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            foo.first(new Runnable() {
                                @Override
                                public void run() {
                                    System.out.print("first");
                                    sleepsometime(sleepTime);
                                }
                            });
                        } catch (InterruptedException e) {
                            //
                        }

                    }
                }).start();

            }
            if (nums[i] == 2) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            foo.second(new Runnable() {
                                @Override
                                public void run() {
                                    System.out.print("second");
                                    sleepsometime(sleepTime);
                                }
                            });
                        } catch (InterruptedException e) {
                            //
                        }
                    }
                }).start();


            }
            if (nums[i] == 3) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            foo.third(new Runnable() {
                                @Override
                                public void run() {
                                    System.out.println("third");
                                    sleepsometime(sleepTime);
                                }
                            });
                        } catch (InterruptedException e) {
                            //
                        }

                    }
                }).start();

            }
        }
    }

    private static void sleepsometime(int sleepTime) {
        try {
//                            System.out.println("first sleep 3 seconds");
            TimeUnit.MILLISECONDS.sleep(sleepTime);
        } catch (InterruptedException e) {
            //
        }
    }
}
